/**
 * 试卷
 */

const router = require('express').Router()
const db = require('../db')
const dbLimitPage = require('../utils').dbLimitPage
// const toWhere = require('../utils').toWhere

// function toDate(time) {
//     if (!time) return false
//     console.log('new Date(time) + ', String(new Date(time)))
//     return (new Date(time) + '').replace('Z', '')
// }

// 保存试卷
router.post('/save', (req, res) => {
    console.log('req.body', req.body)
    let data = req.body
    let { courseId, teacherId } = req.body
    // data.startDate = toDate(data.startDate)
    // data.endDate = toDate(data.endDate)
    // 、、、、、、、、、、、、、、、、、、、、
    data.startDate = null
    data.endDate = null
    // 、、、、、、、、、、、、、、、、、、、、、
    // console.log('data.startDate', data.startDate)
    if (courseId && teacherId && req.query.user.role !== 'student') {
        db.query('INSERT INTO paper SET ?', req.body, result => {
            if (result.affectedRows === 1) {
                res.json({ code: 0, msg: '添加试卷成功', insertId: result.insertId })
            } else {
                res.json({ code: 2, msg: '添加试卷失败' })
            }
        })
    }
})

// 得到试卷列表
router.get('/list', async (req, res) => {
    let { page = 0, limit = 10, user, specialty, classId } = req.query
    if (user.role === 'student') {
        // 拿到专业
        db.query(`SELECT specialty FROM class WHERE ?`, { id: classId }, async (result) => {
            let specialty = result[0]['specialty'];
            let data = await dbLimitPage(`select * from paper_view WHERE specialty = '${specialty}'`, page, limit);
            res.json({ code: 0, page: data })
        })

    }
    else if (user.role === 'teacher') {
        db.query(`SELECT specialty FROM teacher WHERE id = ${user.id}`, {}, result => {
            let specialty = result[0]['specialty'];
            db.query(`SELECT count(*) FROM paper_view WHERE specialty = '${specialty}' and status = 2`, {}, result => {
                let totalCount = result[0]['count(*)'];
                db.query(`SELECT * FROM paper_view WHERE specialty = '${specialty}' and status = 2  LIMIT ${(page - 1) * limit}, ${limit}`, {}, list => {
                    res.json({
                        code: 0,
                        page: {
                            currPage: page,
                            pageSize: limit,
                            list,
                            totalCount,
                            totalPage: Math.ceil(totalCount / limit)
                        }
                    })
                })
            })
        })
    } else {
        let data = await dbLimitPage(`select * from paper_view WHERE specialty = '${specialty}'`, page, limit);
        res.json({
            code: 0,
            page: data
        })
    }
})


// 得到的是固定试题
router.get('/getFixedTest', async (req, res) => {
    let fixed = JSON.parse(req.query.content)
    let data = {}
    for (let tName in fixed) {
        if (fixed[tName].length > 0) {
            data[tName] = []
            let result = await db.querySync(`SELECT * FROM ${tName} where id in  (?)`, [fixed[tName]])
            if (result.length > 0) {
                data[tName] = data[tName].concat(result);
            }
        }
    }
    res.json({ code: 0, data })
})

module.exports = router